package sparrowGui
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	
	import sparrowGui.event.AlertEvent;
	import sparrowGui.vars.AlertVO;

	/**
	 * 警告弹窗管理.
	 * 能弹窗组件是单例的,即当场景只会弹一个,如果是多个提示信息会轮流弹窗
	 * 用前必须先初始 new AlertQueue(stage);
	 * @author Pelephone
	 */
	public class AlertQueue
	{
		protected static var instance : AlertQueue; 
		/**
		 * 警告窗数据队列,这里用静态.
		 */
		private var alertQueue:Array;
		private var _defaultSkin:Sprite;
		private var _defaultBtnCls:Class;
		private var _alt:Alert;
		
		
		/**
		 * 通过提示窗皮肤和你容器构造警告窗体
		 * @param defaultSkin
		 * @param parent
		 */
		public function AlertQueue(parent:DisplayObjectContainer=null,defSkin:Sprite=null,defBtnCls:Class=null )
		{
			if (instance != null) throw Error("此单例已经存在了");
			
			alertQueue = [];
			_alt = new Alert(defSkin,parent,defBtnCls);
			defaultSkin = defSkin || _alt.skin;
			defaultBtnCls = (defBtnCls==null)?_alt.itemClass:defBtnCls;
			instance = this;
		}
		
		//获取单例
		public static function getIns():AlertQueue {
			if (instance == null) instance = new AlertQueue( );
			return instance;
		}
		
		/**
		 * 通过变量弹出提示信息
		 * @param vars
		 */
		public function alertVar(vars:AlertVO):void
		{
			// 如果提示队列还有未关闭的数据就本弹窗信息添加到队列,反之马上弹窗
			if(_alt.isShow)
			{
				alertQueue.push(vars);
				return;
			}
			alertShow(vars);
		}
		
		/**
		 * 马上跟据变量弹窗
		 * @param vars
		 */
		protected function alertShow(vars:AlertVO):void
		{
			//			var avo:AlertVO = alertQueue.shift();
			// 替换警告窗提示
			if(vars.skin && _alt.skin.toString() != vars.skin.toString())
				_alt.reCreate(vars.skin);
			else(!vars.skin && defaultSkin && _alt.skin.toString() != defaultSkin.toString())
			_alt.reCreate(defaultSkin);
			
			// 替换警告窗按钮皮肤
			if(vars.btnCls!=null && String(vars.btnCls)!=String(_alt.itemClass))
				_alt.itemClass = vars.btnCls;
			else(!vars.skin && defaultBtnCls!=null && String(_alt.itemClass) != String(defaultBtnCls))
			_alt.itemClass = defaultBtnCls;
			
			// 如果有输入窗就设置默认输入窗文字
			if(_alt.txtInput && vars.defaultInput)
				_alt.txtInput.text = String(vars.defaultInput);
			
			// 监听界面更新
			if(vars.onUpdate!=null)
				_alt.addEventListener(AlertEvent.ALERT_UPDATE,vars.onUpdate);
			
			_alt.autoClose = vars.autoClose;
			
			_alt.alert(vars.alertData,vars.btnStr,vars.onClose);
			_alt.addEventListener(AlertEvent.ALERT_CLOSE,onAlertClose);
			
			vars.dispose();
		}
		
		/**
		 * 窗体关闭时数值还有警告数据就提示下一项
		 * @param e
		 */
		protected function onAlertClose(e:AlertEvent):void
		{
			if(!_alt.autoClose) return;
			callNext();
		}
		
		/**
		 * 提示下一组警告信息
		 */
		public function callNext():void
		{
			_alt.autoClose = true;
			_alt.removeTypeListeners(AlertEvent.ALERT_CLOSE);
			_alt.removeTypeListeners(AlertEvent.ALERT_UPDATE);
			if(!alertQueue.length) return;
			var alVars:AlertVO = alertQueue.shift();
			alertShow(alVars);
		}
		
		/**
		 * 只有一个确定按钮的提示框
		 * @param txt
		 * @param backFun
		 */
		public function alertOk(txt:String,backFun:Function=null):AlertVO
		{
			return alert(txt,backFun,"确定");
		}
		
		/**
		 * 选择按钮提示框
		 * @param txt
		 * @param backFun
		 */
		public function alertYesNo(txt:String,backFun:Function=null):AlertVO
		{
			return alert(txt,backFun,"确定|取消");
		}
		
		/**
		 * 输入文本
		 * @param txt
		 * @param backFun
		 * @param defaultStr
		 */
		public function alertInput(txt:String,backFun:Function=null,defaultStr:String="请输入数字"):AlertVO
		{
			var btnStr:String = "确定";
			if(!_alt.isShow)
			{
				_alt.alert(txt,btnStr,backFun);
				_alt.addEventListener(AlertEvent.ALERT_CLOSE,onAlertClose);
				return null;
			}
			var vars:AlertVO =  new AlertVO();
			vars.alertData = txt;
			vars.defaultInput = defaultStr;
			vars.onClose = backFun;
			vars.btnStr = btnStr;
			alertVar(vars);
			return vars;
		}
		
		/**
		 * 加按钮参数的警告
		 * @param txt
		 * @param backFun
		 * @param btnStr
		 */
		public function alert(txt:String,backFun:Function=null,btnStr:String="确定"):AlertVO
		{
			if(!_alt.isShow)
			{
				_alt.alert(txt,btnStr,backFun);
				_alt.addEventListener(AlertEvent.ALERT_CLOSE,onAlertClose);
				return null;
			}
			var vars:AlertVO =  new AlertVO();
			vars.alertData = txt;
			vars.onClose = backFun;
			vars.btnStr = btnStr;
			alertVar(vars);
			return vars;
		}

		/**
		 * 警告组件
		 * @return 
		 */
		public function get alertComponent():Alert
		{
			return _alt;
		}

		/**
		 * 重设默认警告窗皮肤
		 */
		public function get defaultSkin():Sprite
		{
			return _defaultSkin;
		}

		/**
		 * @private
		 */
		public function set defaultSkin(value:Sprite):void
		{
			_alt.reCreate(value);
			_defaultSkin = value;
		}

		public function get defaultBtnCls():Class
		{
			return _defaultBtnCls;
		}

		public function set defaultBtnCls(value:Class):void
		{
			_alt.itemClass = value;
			_defaultBtnCls = value;
		}
	}
}